#########################################################################################################
#### Replication File for Uncommon and Nonpartisan: Anti-Democratic Attitudes In The American Public ####
#### By Derek E. Holliday, Shanto Iyengar, Yphtach Lelkes, and Sean J. Westwood                      ####
#### Last Edited: February 8, 2024                                                                   ####
#########################################################################################################

#### Libraries ####
# Uncomment below to install necessary R libraries
#install.packages(c("tidyverse","rio","here","lubridate","pwr","ggthemes","srvyr",
#                   "tidytext","gt","stargazer","lme4","tidymodels","patchwork",
#                   "ranger","feols","kableExtra","survey","vip","hardhat","fixest"))

source(here::here("Code","libraries.R"))

#library(tidytext) # Only used in plotting, called manually
#library(cowplot) # Only used in plotting, called manually

#### Working Directory ####
# Create an R Project such that this file (00_master.R) is in the top-level directory
# Using here() will reference files from the top-level directory

#### Auxiliary Functions ####
# Convenience Function for printing results without warnings:
sx = \(x) suppressWarnings(source(x, print.eval = T))
sx(here("Code","Functions","funcs_theme.R"))
sx(here("Code","Functions","funcs_bvplot.R"))

#### Load Data ####
data_pnas = import(here("Data","data_pnas.rds"))
# Other data are loaded as-needed within the scripts below

#### Main Text: Figures, Tables, In-Text Results ####
# Objects are in order of appearance w/in manuscript

# In-Text: Survey and Panel N
sx(here("Code","Main","gen_intext_n.R"))

# Figure 1: Pooled Norms/Violence Support
sx(here("Code","Main","gen_fig1.R"))

# Figure 2: Norms/Violence Support by Party + Over Time
sx(here("Code","Main","gen_fig2.R"))

# Figure 3: Norms/Violence Support by Party, Strength, and Affective Polarization
sx(here("Code","Main","gen_fig3.R"))

# Figure 4: Perceived Norm/Violence Support by Party
sx(here("Code","Main","gen_fig4.R"))

# Table 1: Multilevel Regression
sx(here("Code","Main","gen_tab1.R"))

# Figure 5: Loyalty Support by Salient Demographic Groups
sx(here("Code","Main","gen_fig5.R"))

# In-Text: Age
sx(here("Code","Main","gen_intext_age.R"))

# In-Text: Power
sx(here("Code","Main","gen_intext_pwr.R"))

#### Supplementary Materials ####

# Figure S1, Table S7: Affective Polarization by Party
sx(here("Code","Supplementary","gen_fig1s.R"))

# Figure S2: Affective Polarization by Strength of Partisanship
sx(here("Code","Supplementary","gen_fig2s.R"))

# Figure S3: Perceived Support by Strength of Partisanship
sx(here("Code","Supplementary","gen_fig3s.R"))

# Tables S2, S3, Figure S4: Number of Norm Violation Support
sx(here("Code","Supplementary","gen_tab2s.R"))

# Figure S5: Pooled Norm violation support (continuous DV)
sx(here("Code","Supplementary","gen_fig5s.R"))

# Figure S6: Norm violation support by party+over time (continuous DV)
sx(here("Code","Supplementary","gen_fig6s.R"))

# Figure S7: Distribution of Continuous Support by Party
sx(here("Code","Supplementary","gen_fig7s.R"))

# Figure S8: Distribution of violence support + difficulty parameter estimates
sx(here("Code","Supplementary","gen_fig8s.R"))

# Figure S9: Ignoring Courts support by Salient Demographic Groups
sx(here("Code","Supplementary","gen_fig9s.R"))

# Figure S10: Censorship support by Salient Demographic Groups
sx(here("Code","Supplementary","gen_fig10s.R"))

# Figure S11: Polling station support by Salient Demographic Groups
sx(here("Code","Supplementary","gen_fig11s.R"))

# Table S4: Survey Toplines
sx(here("Code","Supplementary","gen_tab4s.R"))

# Tables S5 and S6: partisan differences in support - norms/violence
sx(here("Code","Supplementary","gen_tab5s.R"))

# Table S7: See Figure S1

# Tables S8 and S9: Support for norms/violence by week (non-panel)
sx(here("Code","Supplementary","gen_tab8s.R"))

# Tables S10 and S11: Support for norms/violence by week (panel)
sx(here("Code","Supplementary","gen_tab10s.R"))

# Tables S12 and S13: Effect of lagged norms/violence support on current support
sx(here("Code","Supplementary","gen_tab12s.R"))

# Table S14: Norm support by binned affpol (terciles)
sx(here("Code","Supplementary","gen_tab14s.R"))

# Figure S12: Norm support by affpol (LOESS)
sx(here("Code","Supplementary","gen_fig12s.R"))

# Table S15: Multilevel regression (WaPo)
sx(here("Code","Supplementary","gen_tab15s.R"))

# Table S16: Multilevel regression (only 2023)
sx(here("Code","Supplementary","gen_tab16s.R"))

# Table S17: Multilevel regression (Continuous DV)
sx(here("Code","Supplementary","gen_tab17s.R"))

# Table S18: Multilevel regresssion (Any Denier Candidate)
sx(here("Code","Supplementary","gen_tab18s.R"))

# Generate Random Forests
# WARNING #
# The scripts creating random forest outputs are memory intensive (~20 GB). We recommend
# running source .R files one at a time and removing objects from memory you do not need to reuse.
# RF Outputs are pre-generated in the Outputs folder; only run gen_rf.R for replication purposes
sx(here("Code","Supplementary","gen_rf.R"))

# Figure S13: Random forest variable importance (demographics)
sx(here("Code","Supplementary","gen_fig13s.R"))

# Figure S14: Random forest variable importance (all predictors)
sx(here("Code","Supplementary","gen_fig14s.R"))

# Tables S19 and S20: OLS predictors for norms/violence
sx(here("Code","Supplementary","gen_tab19s.R"))

# Figure S15: Random forest model performance (demographics)
sx(here("Code","Supplementary","gen_fig15s.R"))

# Figure S16: Random forest model performance (all predictors)
sx(here("Code","Supplementary","gen_fig16s.R"))

# Table S21: Social desirability robustness
sx(here("Code","Supplementary","gen_tab21s.R"))

# Figure S17: Weight distribution
sx(here("Code","Supplementary","gen_fig17s.R"))


